<?php
/**
 * 《系统日志》实现类
 *
 * @author 孙晓晔
 * @version $Id: LogAction.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/LogUtil.class.php');

class LogAction extends LogUtil {
	public $tbl = Constant::tbl_log_action;

	public $groupby = array(
		'-' => '请选择',
		'uri' => '模块',
		'action' => '行为',
		'ip' => 'IP',
		'emp' => '员工',
		'2' => '日期',
		'3' => '月份',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',
		'uri' => '模块',
		'action' => '行为',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:29:07 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);

		$this->dsn = 'test';
		$this->dsn_w = 'test';
		$this->dsn_r = 'test';
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('uri_cc', Common::cc());
	}

	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tab($controller, $request) {		
		$data = array(
			// '个人动态' => array('cc'=>'emp.php?do=Action.find', 'js'=>''),
			'操作日志' => array('cc'=>'log.php?do=LogAction.find', 'js'=>''),
			'访问日志' => array('cc'=>'log.php?do=LogAccess.find', 'js'=>''),
				
		    '状态信息' => array('cc'=>'log.php?do=Status.find', 'js'=>''),
			'IP分配' => array('cc'=>'emp.php?do=Mac.find', 'js'=>'1'),
		    
			'网站浏览' => array('cc'=>'log.php?do=Web.find', 'js'=>''),
					
			'站内消息' => array('cc'=>'log.php?do=Message.find', 'js'=>''),
			'站内通知' => array('cc'=>'log.php?do=Notice.find', 'js'=>''),
			'每日工作' => array('cc'=>'log.php?do=Job.find', 'js'=>''),

			'统计分析' => array('cc'=>'log.php?do=LogAction.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');		
		return true;
	}
	
	function tj_tab($controller, $request) {
		$data = array(
			'操作日志' => array('cc'=>'log.php?do=LogAction.group', 'js'=>''),
			'访问日志' => array('cc'=>'log.php?do=LogAccess.group', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
			
		$controller->assign('t1', strtotime('-1 day'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/action/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {

		$cc_s = $request['cc_s'];
		$action_s = $request['action_s'];
		$ip_s = $request['ip_s'];

		$where = " WHERE 1=1";
		if (!empty($cc_s) && $cc_s <> '-') {
			$where .= " AND uri like '%" . $cc_s . "'";
		}

		if (!empty($action_s)) {
			$where .= " AND action like '" . $action_s . "%'";
		}

		if (!empty($ip_s)) {
			$where .= " AND ip like '" . $ip_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'log/action/list');
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);
	
		$row = $this->_view($request['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'log/action/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);
		$row['ip_x'] = $this->_emp_ip($row['ip']);

		return $row;
	}
	
	function _insert($request) {
		$action = $request['do'];
	
		if (preg_match('/^(LogAction|LogAccess)/i', $action)) {
			return;
		}
		
		if (preg_match('/^(NoticeMy\.my)/i', $action)) {
			return;
		}
	
		if (preg_match('/(\.ajax_)/i', $action)) {
			return;
		}
	
		if (preg_match('/(audit)$/i', $action)) {
			return;
		}
	
		$tpl = $request['tpl'];
	
		$uri = $_SERVER["PHP_SELF"];
		$query = $_SERVER["QUERY_STRING"];
		$content = json_encode($request);
		
		$ip	= $_SERVER['REMOTE_ADDR'];
		$ua = $_SERVER['HTTP_USER_AGENT'];
		$emp = $_SESSION['id'];
	
		$totaltime = $this->_totaltime($request);
	
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(uri, action, query, content, tpl, totaltime, ip, ua, emp, createtime) 
				VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $uri, $action, $query, $content, $tpl, $totaltime, $ip, $ua, $emp);
		// debug($sql);
		$db->query($sql);
	}
	
	function _totaltime($request) {
		$endtime = cur_time();
		$totaltime = $endtime - $request['starttime'];
	
		return number_format($totaltime, 7);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-7 day'));
		$controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/action/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} elseif ($px == 'ip') {
			$row['a'] = $this->name_ip($row['a']);
		} else {
		
			$row = $this->_name_t($row, $px);
		}
		
		return $row;
	}

	function name_ip($ip) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE ip = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_mac, $ip);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return sprintf("%s[%s]", Name::emp($row['emp']), $ip);
		}

		return $ip;
	}

	/**
	 * 
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tj($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$subject = '系统使用情况';

		$body = $this->_tj_a($controller, $request);
		
		$bb = $this->_list_email('339');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function _tj_a($controller, $request) {
		$this->pretreat($controller);

		$groupby_s = 'emp';
		$s = $this->groupby($controller, $groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$db = newdb($this->dsn_r);
		$sql = "SELECT $s AS a, ip, COUNT($s) AS c FROM " . $this->tbl . " a ";
		$where = " WHERE createtime > " . $df;

		$orderby = ' ORDER BY c DESC';

		$groupby = " GROUP BY $s, ip";
		$sql .= $where . $groupby . $orderby;
		debug($sql);
		$result = $db->query($sql);

		$data = array();
		$count = 0;
		$xx = 0;
		$total = 0;
		while ($row = $db->fetch_array($result)) {
			if ($row['a'] == '214') {
				continue;
			}

			$total += $row['c'];
			if ($count > 30) {
				$xx += $row['c'];
				continue;
			}

			if (empty($row['a'])) {
				continue;
			}

			$row = $this->_name($row, $groupby_s);
				
			$row['ip_t'] = $this->name_ip($row['ip']);
				
			$data[] = $row;
			$count++;
		}

		if ($xx > 0) {
			$data[] = array('a'=>'其它', 'c'=>$xx);
		}

		$aa = array();
		while (list($k, $v) = each($data)) {
			$v['p'] = number_format(($v['c'] / $total) * 100, 2, '.', '') . "%";
			$aa[] = $v;
		}
			
		$a = array();
		$c = array();
		reset($data);
		while (list($k, $v) = each($data)) {
			$a[] = $v['a'];
			$c[] = $v['c'];
		}
			
		$sa = join(",", $a);
		// echo "\n";
		$sc = join(",", $c);
			
		$sa = trim($sa, ",");
			
		$controller->assign('sa', $sa);
		$controller->assign('sc', $sc);

		$controller->assign_by_ref('total', $total);

		$controller->assign_by_ref('result', $aa);

		return $controller->sunxy($request, 'log/action/tj_a');
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'log/action/_list');
		return true;
	}
}
?>
